/*
 -----------------------------------------------------------------------------
 This source file is part of FolderProtector
 Copyright (c) 2011, Vyacheslav Chigrin, folderprotector (at) gmail.com  All rights reserved.
 Send your commends
	 ---The BSD 3-Clause License---
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.
* Neither the name of the Folder Protector project nor the names of its contributors
  may be used to endorse or promote products derived from this software without specific
  prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  -----------------------------------------------------------------------------
*/
#include "stdafx.h"
#include "PathsMatcher.h"

CPathsMatcher::CPathsMatcher()
    : m_bIsEnabled(false)
    , m_PathsCount(0)
    , m_pPaths(NULL)
{
}

void CPathsMatcher::Destroy()
{
    FreePaths();
}

bool CPathsMatcher::IsPathMatches(UNICODE_STRING *pPath)const
{
    if(!m_bIsEnabled)
        return false;
    for(int i=0;i<m_PathsCount;i++)
    {
        if(FsRtlIsNameInExpression(&m_pPaths[i],pPath,TRUE,NULL))
            return true;
    }
    return false;
}

bool CPathsMatcher::InitPaths(const WCHAR* pRegValue)
{
    FreePaths();
    int pathsCount=0;
    const WCHAR* p=pRegValue;
    //count number of paths in value
    while(*p)
    {
        p+=wcslen(p)+1;
        pathsCount++;
    }
    m_pPaths=(UNICODE_STRING*)ExAllocatePoolWithTag(PagedPool,sizeof(UNICODE_STRING)*pathsCount,POOL_TAG);
    if(!m_pPaths)
    {
        KdPrint(("Failed allocate pool for paths"));
        return false;
    }
    m_PathsCount=pathsCount;
    memset(m_pPaths,0,sizeof(UNICODE_STRING)*pathsCount);
    int index=0;
    p=pRegValue;
    while(*p)
    {
        int lenChars=wcslen(p)+1;
        int sizeBytes=lenChars*sizeof(WCHAR);
        m_pPaths[index].Buffer=(WCHAR*)ExAllocatePoolWithTag(PagedPool,sizeBytes,POOL_TAG);
        if(!m_pPaths[index].Buffer)
        {
            KdPrint(("Failed allocate pool for path"));
            FreePaths();
            return false;
        }
        m_pPaths[index].Length=sizeBytes-sizeof(WCHAR);
        m_pPaths[index].MaximumLength=(USHORT)sizeBytes;
        //to perform case-insensitive matching, we must convert paths to upper case
        UNICODE_STRING tmp;
        tmp.Buffer=(WCHAR*)p;
        tmp.MaximumLength=(USHORT)sizeBytes;
        tmp.Length=sizeBytes-sizeof(WCHAR);
        NTSTATUS status=RtlUpcaseUnicodeString(&m_pPaths[index],&tmp,FALSE);
        if(!NT_SUCCESS(status))
        {
            KdPrint(("Failed convert string %08X",status));
            FreePaths();
            return false;
        }
        //it's better to have zero terminator (may be used somewhere)
        m_pPaths[index].Buffer[m_pPaths[index].MaximumLength/sizeof(WCHAR)-1]=L'\0';
        KdPrint(("Added path %wZ",&m_pPaths[index]));
        index++;
        p+=lenChars;
    }
    return true;
}

void CPathsMatcher::Enable(bool isEnabled)
{
    m_bIsEnabled=isEnabled;
}

void CPathsMatcher::FreePaths()
{
    if(!m_pPaths)
        return;
    for(int i=0;i<m_PathsCount;i++)
    {//check to allow call in case when not all paths initialized properly (in case of allocation fail during initialization)
        if(m_pPaths[i].Buffer)
            ExFreePool(m_pPaths[i].Buffer);
    }
    ExFreePool(m_pPaths);
    m_pPaths=NULL;
    m_PathsCount=0;
}
